<?php

	class User {
		
		public function __construct() {

			$this->Id = "";
			$this->PreviousId = "";		
			$this->Password = "";
			$this->Name = "";
			$this->EmailAddress = "";
			$this->TelOffice = "";
			$this->TelOffice = "";					
		}

		function SetId($user_id) {
			$this->PreviousId = $this->Id;
			$this->Id = strtoupper(substr($user_id,0,50));
		}

		function SetPassword($user_password) {
			$this->Password = substr($user_password,0,50);
		}

		function SetName($user_name) {
			$this->Name = substr($user_name,0,255);
		}

		function SetLevel($user_level) {
			$this->Level = $user_level;
		}

		function SetEmailAddress($user_email_address) {
			$this->EmailAddress = substr($user_email_address,0,255);
		}

		function SetTelOffice($user_tel_office) {
			$this->TelOffice = substr($user_tel_office,0,50);
		}

		function SetTelMobile($user_tel_mobile) {
			$this->TelMobile = substr($user_tel_mobile,0,50);
		}
		
		public function GetId() {
			return $this->Id;
		}

		public function GetPassword() {
			return $this->Password;
		}

		public function GetName() {
			return $this->Name;
		}		

		public function GetLevel() {
			return $this->Level;
		}	

		public function GetEmailAddress() {
			return $this->EmailAddress;
		}

		public function GetTelOffice() {
			return $this->TelOffice;
		}

		public function GetTelMobile() {
			return $this->TelMobile;
		}

		function Load($user_id) {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = strtoupper(substr($user_id,0,50));
	
			mysql_connect($db_host,$db_username,$db_password);
			@mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "SELECT * FROM user WHERE user_id = '$user_id';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			mysql_close();	
			
			if($db_count>0) {
			
				$user_id				= mysql_result($db_result,0,"user_id");
				$user_password			= mysql_result($db_result,0,"user_password");
				$user_name				= mysql_result($db_result,0,"user_name");
				$user_level				= mysql_result($db_result,0,"user_level");
				$user_email_address		= mysql_result($db_result,0,"user_email_address");
				$user_tel_office		= mysql_result($db_result,0,"user_tel_office");
				$user_tel_mobile		= mysql_result($db_result,0,"user_tel_mobile");
								
				$this->Id = strtoupper($user_id);				
				$this->Password = $user_password;
				$this->Name = $user_name;	
				$this->Level = $user_level;
				$this->EmailAddress = $user_email_address;	
				$this->TelOffice = $user_tel_office;	
				$this->TelMobile = $user_tel_mobile;									
				
				return true;			
	
			} else {
			
				$this->Id = "";
				$this->Password = "";
				$this->Name = "";
				$this->Level = 0;
				$this->EmailAddress = "";
				$this->TelOffice = "";
				$this->TelMobile = "";
				
				return false;
				
			}
		
		}
		
		function Save() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = $this->Id;
			$user_password = $this->Password;
			$user_name = $this->Name;
			$user_level = $this->Level;
			$user_email_address = $this->EmailAddress;
			$user_tel_office = $this->TelOffice;
			$user_tel_mobile = $this->TelMobile;
			$user_previous_id = $this->PreviousId;
			
			$user_id = strtoupper(substr($user_id,0,50));
			$user_password = substr($user_password,0,50);
			$user_name = substr($user_name,0,255);		
			$user_email_address = substr($user_email_address,0,255);
			$user_tel_office = substr($user_tel_office,0,50);
			$user_tel_mobile = substr($user_tel_mobile,0,50);
					
			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			
			$db_query = "SELECT * FROM user WHERE user_id = '$user_previous_id';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			
			if($db_count==0) {
				
				$db_query = "INSERT INTO user VALUES ('$user_id','$user_password','$user_name',$user_level,'$user_email_address','$user_tel_office','$user_tel_mobile');";
				mysql_query($db_query);
				
			} else {
			
				$db_query = "UPDATE user SET user_id = '$user_id', user_password = '$user_password', user_name = '$user_name', user_level = $user_level, user_email_address = '$user_email_address', user_tel_office = '$user_tel_office', user_tel_mobile = '$user_tel_mobile' WHERE user_id = '$user_previous_id';";
				mysql_query($db_query);
			
			}
			
			mysql_close();
		
		}

		function Delete() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = $this->Id;			
					
			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "DELETE FROM user WHERE user_id = '$user_id';";
			mysql_query($db_query);
			mysql_close();
		
		}	

		function IsInTeam($team_name) {
			
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = $this->Id;
			$team_name = strtoupper(substr($team_name,0,50));			

			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");

			$db_query = "SELECT * FROM user_team WHERE user_id = '$user_id' AND team_name = '$team_name';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			
			if($db_count>0) {
				return true;
			} else {
				return false;
			}
			
		}
		
		function AddToTeam($team_name) {
			
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = $this->Id;
			$team_name = strtoupper(substr($team_name,0,50));			

			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");

			$db_query = "SELECT * FROM team WHERE team_name = '$team_name';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);

			if($db_count>0) {

				$db_query = "INSERT INTO user_team VALUES ('$user_id','$team_name');";
				mysql_query($db_query);
				
			
			}
			
		}

		function RemoveFromTeam($team_name) {
			
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = $this->Id;
			$team_name = strtoupper(substr($team_name,0,50));			

			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");

			$db_query = "DELETE FROM user_team WHERE user_id = '$user_id' AND team_name = '$team_name';";
			$db_result = mysql_query($db_query);
			
			mysql_close();
						
		}
	
	}

?>
